package com.zoweunion.mechanic.controller.base;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.dao.base.LockCarMapper;
import com.zoweunion.mechanic.entity.ThingsIdCarRecordBean;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.model.app.LockCar;
import com.zoweunion.mechanic.service.base.LockCarService;
import com.zoweunion.mechanic.util.ExcelUtils;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("lockCar")
public class LockCarController extends BaseController {
    @Autowired
    LockCarService lockCarService;

    @Autowired
    LockCarMapper lockCarMapper;

    /**
     * 获取锁车信息
     *
     * @return
     */
    @PostMapping("getLockCar")
    public ResponseBean getLockCar(@RequestBody LockCar lockCar, HttpServletRequest request) throws MyException, InvocationTargetException, IllegalAccessException {
        logBefore(logger, "锁车操作");
        User currentUser = getCurrentUser(request);
        Map<String, Object> map = lockCarService.getLockCar(currentUser, lockCar);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", map);
    }

    /**
     * 更改监控状态(MonitorState)的值
     *
     * @return
     */
    @PostMapping("changeMonitorState")
    public ResponseBean changeMonitorState(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException, InvocationTargetException, IllegalAccessException {
        logBefore(logger, "更改监控状态(MonitorState)");
        lockCarService.changeMonitorState(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 锁车
     *
     * @return
     */
    @PostMapping("lock")
    public ResponseBean lock(@RequestBody LockCar lockCar, HttpServletRequest request) throws MyException, InvocationTargetException, IllegalAccessException {
        logBefore(logger, "锁车操作");
        User currentUser = getCurrentUser(request);
        lockCarService.lockCar(currentUser, lockCar);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 解锁
     *
     * @return
     */
    @PostMapping("unlock")
    public ResponseBean unlock(@RequestBody LockCar lockCar, HttpServletRequest request) throws MyException {
        logBefore(logger, "锁车操作");
        User currentUser = getCurrentUser(request);
        lockCarService.unlockCar(currentUser, lockCar);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 取消锁车
     *
     * @return
     */
    @PostMapping("cancelLock")
    public ResponseBean cancelLock(@RequestBody LockCar lockCar, HttpServletRequest request) throws MyException {
        logBefore(logger, "锁车操作");
        User currentUser = getCurrentUser(request);
        lockCarService.cancelLockCar(currentUser, lockCar);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 提交疑问
     *
     * @return
     */
    @PostMapping("commitQuestion")
    public ResponseBean commitQuestion() {
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 申请解锁
     *
     * @return
     */
    @PostMapping("applicationUnlock")
    public ResponseBean applicationUnlock(@RequestBody String str,HttpServletRequest request) {

        try {

            //car_id
            Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);

            User user = getCurrentUser(request);

            reqMap.put("user_id",user.getId());

            reqMap.put("s_id",user.getS_id());

            int result = lockCarService.insertAppleLock(reqMap);

            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);

        } catch (Exception e) {

            System.out.println(e.getMessage());
            return new ResponseBean(Constants.CODE_HANDEL_FAILED, "操作失败", null);
        }
    }

    /**
     * 获取被锁车列表
     *
     * @return
     */
    @PostMapping("getLockVehicleList")
    public ResponseBean getLockVehicleList(HttpServletRequest request) throws MyException {
        logBefore(logger, "获取被锁车列表");
        User user = getCurrentUser(request);
        Map<String, Object> reqMap = new HashMap<>(3);
        reqMap.put("o_id", user.getId());
        Map<String, Object> resultMap = lockCarService.getLockVehicleList(user, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 获取申请解锁列表
     *
     * @return
     */
    @PostMapping("getApplicationUnlockList")
    public ResponseBean getApplicationUnlockList(@RequestBody String requestBody, HttpServletRequest request) throws MyException {
        User user = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(requestBody, Map.class);
        List<Map<String, Object>> resultMap = lockCarService.getApplicationUnlockList(user, reqMap);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 车辆命令插入
     * 可以解锁
     * @return
     */
    @PostMapping("thingsIdCarOperation")
    public ResponseBean thingsIdCarOperation(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "车辆命令插入");
        User currentUser = getCurrentUser(request);
        reqMap.put("lock_user_id", currentUser.getId());
        reqMap.put("create_user", currentUser.getId());
        reqMap.put("update_user", currentUser.getId());
        reqMap.put("s_id", currentUser.getS_id());
        reqMap.put("user_name", currentUser.getUser_name());

        lockCarService.thingsIdCarOperation(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 蓝牙解锁发送
     *
     * @return
     */
    @GetMapping("bluetoothUnLock")
    public ResponseBean bluetoothUnLock(@RequestParam Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "蓝牙解锁发送");
        lockCarService.bluetoothUnLock(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", true);
    }

    /**
     * 锁车记录查询
     *
     * @return
     */
    @PostMapping("thingsIdCarRecord")
    public ResponseBean thingsIdCarRecord(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "锁车记录查询");

        if(reqMap.get("things_id").equals("18201111061")) {  // 湖北黄冈的车辆新安装的设备(18211810161)与信昌的设备(18201111061)进行了调换(新安装的设备((18211810161))可以锁车，信昌的设备(18201111061)只有数据)
            reqMap.put("things_id", "18211810161");
        }
        List<Map<String, Object>> thingsIdCarRecord = lockCarService.thingsIdCarRecord(reqMap);

        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", thingsIdCarRecord);
    }

    /**
     * 周期锁车命令获取
     * @return
     */
    @PostMapping("getCycleLockThingsIdList")
    public ResponseBean getCycleLockThingsIdList(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "周期锁车命令获取");
        List<Map<String, Object>> result = lockCarService.getCycleLockThingsIdList(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 更新周期锁车的信息的执行状态
     * @return
     */
    @PostMapping("updateCycleLockThingsStatus")
    public ResponseBean updateCycleLockThingsStatus(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "更新周期锁车的信息的执行状态");
        int result = lockCarService.updateCycleLockThingsStatus(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 更新周期锁车的信息的执行状态
     * @return
     */
    @PostMapping("updateCarStatusForCycleLock")
    public ResponseBean updateCarStatusForCycleLock(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "更新周期锁车的车辆设备的锁车状态");
        int result = lockCarService.updateCarStatusForCycleLock(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 更新周期锁车的信息的执行状态 - 用于APP解锁页面
     * @return
     */
    @PostMapping("updateCycleLockThingsStatusForUnlock")
    public ResponseBean updateCycleLockThingsStatusForUnlock(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "更新周期锁车的信息的执行状态 - 用于APP解锁页面");
        int result = lockCarService.updateCycleLockThingsStatusForUnlock(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 导出-锁车记录查询
     *
     * @return
     */
    @GetMapping("exportThingsIdCarRecord")
    public void exportThingsIdCarRecord(@RequestParam Map<String, Object> reqMap, HttpServletResponse response) throws Exception {
        logBefore(logger, "导出-锁车记录查询");
        List<ThingsIdCarRecordBean> thingsIdCarRecord = lockCarService.exportThingsIdCarRecord(reqMap);
        logAfter(logger);
        ExcelUtils.exportExcelToTarget(response, "锁车记录", thingsIdCarRecord, ThingsIdCarRecordBean.class);
    }

    /**
     * 锁车记录查询
     *
     * @return
     */
    @PostMapping("thingsIdCarRecordNew")
    public ResponseBean thingsIdCarRecordNew(@RequestBody Map<String, Object> reqMap) {
        logBefore(logger, "锁车记录查询");
        Map<String, Object> thingsIdCarRecordNew = lockCarService.thingsIdCarRecordNew(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", thingsIdCarRecordNew);
    }
}
